package com.free.semantic.core.impl.demo;

import com.free.semantic.common.LocatorNode;
import com.free.semantic.common.Ls;
import com.free.semantic.common.ParamNode;
import com.free.semantic.common.RelationChain;
import com.free.semantic.core.ConceptInstanceManage;
import com.free.semantic.core.ConceptManage;
import com.free.semantic.core.impl.BaseTest;
import com.free.semantic.core.impl.ConceptInstanceManageImpl;
import com.free.semantic.core.impl.ConceptManageImpl;
import com.free.semantic.req.RelationDefineGroup;
import org.junit.jupiter.api.Test;

import java.util.List;

import static com.free.semantic.common.RangeObj.one;


public class Demo8_newSearchMethod extends BaseTest {


    @Test
    void test1() {

        RelationDefineGroup group = new RelationDefineGroup();
        defineRelation(group, "找到",
                defineItem(oneObj("A"), oneObj("B"))); //B->A=1  //代表 针对任意一个B，都可以找到 1个A与之对应

        ConceptManageImpl one = (ConceptManageImpl) getConceptManage(group);

        one.addConcept("公司", "支付单", "采购单", "编号");

        createRelation(one, "找到", "公司", "支付单");

        createRelation(one, "找到", "公司", "采购单");
        createRelation(one, "找到", "采购单", "编号");
        createRelation(one, "找到", "支付单", "编号");

        RelationChain chain = one.findChainByLocatorNode(Ls.of(P("公司"), L("支付单")), one("编号"));

        System.out.println(chain);

        assertObj(chain.getSourceObjectList(), "公司");
        assertObj(chain.getDestObject(), "编号");
    }


    @Test
    void test2() {

        RelationDefineGroup group = new RelationDefineGroup();
        defineRelation(group, "找到",
                defineItem(oneObj("A"), oneObj("B"))); //B->A=1  //代表 针对任意一个B，都可以找到 1个A与之对应

        ConceptManageImpl one = (ConceptManageImpl) getConceptManage(group);
        one.addConcept("公司", "支付单", "采购单", "编号");

        createRelation(one, "找到", "公司", "支付单");

        createRelation(one, "找到", "公司", "采购单");
        createRelation(one, "找到", "采购单", "编号");
        createRelation(one, "找到", "支付单", "编号");
        try {
            RelationChain chain = one.findChainByLocatorNode(Ls.of(P("公司"), P("支付单")), one("编号"));
            System.out.println(chain);
        } catch (RuntimeException e) {
            assert e.getMessage().equals("不存在从[公司,支付单]到[编号]的路径");
        }
    }


    /**
     * 对于一个输入有两个的节点还是需要再次测试下
     */
    @Test
    void test3() {


    }


    /**
     * 最重要的是实现递归查询
     */
    @Test
    void test4() {

        RelationDefineGroup group = new RelationDefineGroup();
        defineRelation(group, "找到",
                defineItem(oneObj("A"), oneObj("B"))); //B->A=1  //代表 针对任意一个B，都可以找到 1个A与之对应

        ConceptManage one = getConceptManage(group);
        one.addConcept("人", "妈妈", "年龄");

        createRelation(one, "找到", "人", "妈妈");
        createRelation(one, "找到", "妈妈", "人");
        createRelation(one, "找到", "人", "年龄");

        //创建模型的一个实现，并且实现 省份查询城市  和  城市查询 省份 函数（添加一些测试数据）
        ConceptInstanceManage instance = new ConceptInstanceManageImpl(one,
                buildMap("人", "妈妈", new String[][]{
                        {"张三", "李四"},
                        {"李四", "王五"},
                        {"王五", "赵六"}}),
                buildMap("妈妈", "人", new String[][]{
                        {"张三", "张三"},
                        {"李四", "李四"},
                        {"王五", "王五"},
                        {"赵六", "赵六"}}),
                buildMap("人", "年龄", new String[][]{
                        {"张三", "12"},
                        {"李四", "23"},
                        {"王五", "34"},
                        {"赵六", "45"}}));

        List<String> objects = instance.goByLocatorNode(Ls.of(
                new ParamNode("人", "张三"), new LocatorNode("妈妈"),
                new LocatorNode("人"), new LocatorNode("妈妈"),
                new LocatorNode("人"), new LocatorNode("妈妈")), one("年龄"));

        assert objects.get(0).equals("45");


    }


}